package com.lindroy.jetpacknotes.viewmodel

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.doAfterTextChanged
import androidx.lifecycle.ViewModelProvider
import com.lindroy.jetpacknotes.R
import kotlinx.android.synthetic.main.activity_edit_text.*

/**
 * @author Lin
 * @date 2022/11/21
 * @function
 */
class EditTextViewModelActivity : AppCompatActivity() {

    /**
     * 创建一个 ViewModel 实例
     */
    /*private val viewModel by lazy {
        ViewModelProvider(this)[EditTextViewModel::class.java]
    }*/

    private val viewModel by viewModels<EditTextViewModel>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit_text)
        //恢复数据
        etInput.setText(viewModel.userInput)
        //输入框的文字变化时保存数据
        etInput.doAfterTextChanged {
            viewModel.userInput = it.toString()
        }
    }
}